/* entity */
import { QualityInfoForm, QualityInfoFormGroup, QualityInfoOriginal } from '@model/entity/QualityInfo'
import Field from '@model/entity/Field'
/* enum */
import { QualityInfoStateEnum, QualityInfoStateValueEnum, QualityInfoTypeEnum, QualityInfoTypeValueEnum } from '@model/enum/QualityInfoEnum'
import { ProductFieldNameMappingEnum } from '@model/enum/FieldMappingEnum'
import DateFormatEnum from '@model/enum/DateFormatEnum'
/* service */
import { isQualityInfoField } from '@service/FieldService'
/* util */
import { cloneDeep } from 'lodash'
import { useQualityInfoComputedRuleState, useQualityInfoFormViewGroupFields, useQualityInfoTableGroupFields } from '@hooks/useQualityInfo'
/* vue */
import { ComputedRef } from 'vue'
import moment from 'moment'

const QualityInfoStatusSearchMap: Record<string, any> = {
  [QualityInfoStateEnum.In]: 1,
  [QualityInfoStateEnum.Out]: 0
}

/**
 * @description 是否为质保信息 自动计算
 * @param {QualityInfoTypeEnum} value 质保期值
 * @return {Boolean}
 */
export function isAutoComputedQualityInfo(value: QualityInfoTypeEnum | null): boolean {
  return value === QualityInfoTypeEnum.Auto
}

/**
 * @description 是否为质保信息 手动添加
 * @param {QualityInfoTypeEnum} value 质保期值
 * @return {Boolean}
 */
export function isManualAddQualityInfo(value: QualityInfoTypeEnum | null): boolean {
  return value === QualityInfoTypeEnum.Manual
}

/**
 * @description 是否为质保信息 自动计算 的值
 * @param {QualityInfoTypeEnum} value 质保期值
 * @return {Boolean}
 */
export function isAutoComputedQualityInfoValue(value: QualityInfoTypeValueEnum | string | null): boolean {
  return value === QualityInfoTypeValueEnum.Auto
}

/**
 * @description 是否为质保信息 手动添加的值
 * @param {QualityInfoTypeEnum} value 质保期值
 * @return {Boolean}
 */
export function isManualAddQualityInfoValue(value: QualityInfoTypeValueEnum | string | null): boolean {
  return value === QualityInfoTypeValueEnum.Manual
}

/**
 * @description: 服务端质保信息数据转成表单质保信息数据
 * @param {QualityInfoOriginal} qualityRuleRemoteData 服务端质保信息数据
 * @param {Boolean} isComputedQualityInfoStatus 是否自动计算质保信息状态
 * @return {QualityRuleForm} 表单质保信息数据
 */
export function convertQualityInfoRemoteDataToFormQualityInfoFieldValue(
  qualityInfoRemoteData: QualityInfoOriginal | Record<string, any>,
  isComputedQualityInfoStatus: boolean = false
): QualityInfoForm {
  let qualityStartTime: string = qualityInfoRemoteData?.qualityStartTime || ''
  let qualityEndTime: string = qualityInfoRemoteData?.qualityEndTime || ''
  return {
    // 质保开始时间
    [ProductFieldNameMappingEnum.QualityInfoStartTime]: qualityStartTime,
    // 质保结束日期
    [ProductFieldNameMappingEnum.QualityInfoEndTime]: qualityEndTime,
    // 质保状态
    [ProductFieldNameMappingEnum.QualityInfoStatus]: (
      isComputedQualityInfoStatus
        ? useQualityInfoComputedRuleState(qualityStartTime, qualityEndTime)
        : qualityInfoRemoteData?.qualityStatus || ''
    )
  }
}

/**
 * @description: 表单质保信息数据转成服务端质保信息数据
 * @param {QualityRuleForm} QualityInfoForm
 * @return {QualityRuleOriginal} 服务端质保信息数据
 */
export function convertFormQualityInfoFieldValueToQualityInfoRemoteData(
  formQualityInfoFieldValue: QualityInfoForm | Record<string, any>
): QualityInfoOriginal {  
  
  // 质保开始日期
  const qualityStartTime: string = formQualityInfoFieldValue?.[ProductFieldNameMappingEnum.QualityInfoStartTime] || ''
  // 质保结束日期
  const qualityEndTime: string = formQualityInfoFieldValue?.[ProductFieldNameMappingEnum.QualityInfoEndTime] || ''
  
  return {
    // 质保开始时间
    qualityStartTime,
    // 质保结束日期
    qualityEndTime,
    // 质保状态
    qualityStatus: formQualityInfoFieldValue?.[ProductFieldNameMappingEnum.QualityInfoStatus] || ''
  }
}

/**
 * @description: 表单质保信息分组数据转成服务端质保信息数据
 * @param {QualityInfoFormGroup} formQualityInfoGroupValue 质保信息组数据
 * @return {QualityRuleOriginal} 服务端质保信息数据
 */
export function convertFormQualityInfoGroupValueToQualityInfoRemoteData(
  formQualityInfoGroupValue: QualityInfoFormGroup,
  originProduct: Record<string, any>
): QualityInfoOriginal {
  
  let formQualityInfoFieldValue: QualityInfoForm | Record<string, any> = {}
  let isAutoComputed = Boolean(formQualityInfoGroupValue?.isAutoComputed)
  let isManualAdd = Boolean(formQualityInfoGroupValue?.isManualAdd)
  
  if (isAutoComputed) {
    formQualityInfoFieldValue = formQualityInfoGroupValue?.auto || {}
  } 
  else if (isManualAdd) {
    formQualityInfoFieldValue = formQualityInfoGroupValue?.manual || {}
  }
  
  const qualityInfoRemoteData: QualityInfoOriginal = convertFormQualityInfoFieldValueToQualityInfoRemoteData(formQualityInfoFieldValue)
  
  return Object.assign(
    qualityInfoRemoteData, 
    {
      qualityCalculation: getQualityCalculationDefault(formQualityInfoGroupValue)
    }
  )
}

export function getQualityCalculationDefault(
  formQualityInfoGroupValue: QualityInfoFormGroup
): QualityInfoTypeValueEnum | string {
  let isAutoComputed = Boolean(formQualityInfoGroupValue?.isAutoComputed)
  let isManualAdd = Boolean(formQualityInfoGroupValue?.isManualAdd)
  
  return (
    isAutoComputed 
      ? QualityInfoTypeValueEnum.Auto 
      : isManualAdd 
        ? QualityInfoTypeValueEnum.Manual 
        : ''
  )
}

/**
 * @description: 表单 服务端质保信息数据 转成 质保信息分组数据
 * @param {QualityInfoFormGroup} formQualityInfoGroupValue 服务端质保信息数据
 * @param {Boolean} isComputedQualityInfoStatus 是否自动计算质保信息状态
 * @return {QualityRuleOriginal} 质保信息分组数据
 */
export function convertQualityInfoRemoteDataToFormQualityInfoGroupValue(
  qualityInfoRemoteData: QualityInfoOriginal,
  isComputedQualityInfoStatus: boolean = false,
): QualityInfoFormGroup {
  // 质保期计算方式
  const qualityCalculation: string = qualityInfoRemoteData?.qualityCalculation || ''
  
  return {
    auto: convertQualityInfoRemoteDataToFormQualityInfoFieldValue(qualityInfoRemoteData, isComputedQualityInfoStatus),
    manual: convertQualityInfoRemoteDataToFormQualityInfoFieldValue(qualityInfoRemoteData, isComputedQualityInfoStatus),
    isAutoComputed: isAutoComputedQualityInfoValue(qualityCalculation),
    isManualAdd: isManualAddQualityInfoValue(qualityCalculation)
  }
}

/**
 * @description 构建质保信息表单数据
 * @param {String} startTime 质保开始时间 
 * @param {String} endTime 质保结束时间 
 * @param {String} state 质保状态 
 * @return {QualityInfoForm} 质保信息表单数据
 */
export function buildQualityInfoForm(startTime: string | undefined, endTime: string | undefined, state: string | undefined| QualityInfoTypeEnum): QualityInfoForm {
  return {
    // 质保开始时间
    [ProductFieldNameMappingEnum.QualityInfoStartTime]: startTime || '',
    // 质保结束日期
    [ProductFieldNameMappingEnum.QualityInfoEndTime]: endTime || '',
    // 质保状态
    [ProductFieldNameMappingEnum.QualityInfoStatus]: state || ''
  }
}

/**
 * @description 产品类型质保信息字段展开给表单显示用的
 * @return {Field[]} 展开的字段列表
 */
export function smoothQualityInfoFieldForFormView(fields: Field[]): Field[] {
  const originFields: Field[] = cloneDeep(fields)
  // 质保信息字段索引
  const qualityInfoFieldIndex: number = originFields.findIndex((field: Field) => isQualityInfoField(field))
  // 无质保信息字段则返回
  if (qualityInfoFieldIndex < 0) return originFields
  
  // 质保信息字段
  const qualityInfoField: Field = fields[qualityInfoFieldIndex]
  // 质保信息字段列表
  const qualityInfoFields: ComputedRef<Field[]> = useQualityInfoFormViewGroupFields(qualityInfoField)
  
  // 替换质保字段为质保字段列表(平铺)
  fields
  .splice(
    qualityInfoFieldIndex,
    1,
    ...qualityInfoFields.value
  )
  
  return (
    fields.filter(field => Boolean(field))
  )
}

/**
 * @description 产品类型质保信息字段展开给表格显示用的
 * @return {Field[]} 展开的字段列表
 */
export function smoothQualityInfoFieldForTable(fields: Field[]): Field[] {
  const originFields: Field[] = cloneDeep(fields)
  // 质保信息字段索引
  const qualityInfoFieldIndex: number = originFields.findIndex((field: Field) => isQualityInfoField(field))
  // 无质保信息字段则返回
  if (qualityInfoFieldIndex < 0) return originFields
  
  // 质保信息字段
  const qualityInfoField: Field = fields[qualityInfoFieldIndex]
  // 质保信息字段列表
  const qualityInfoFields: ComputedRef<Field[]> = useQualityInfoTableGroupFields(qualityInfoField)
  
  // 替换质保字段为质保字段列表(平铺)
  fields
  .splice(
    qualityInfoFieldIndex,
    1,
    ...qualityInfoFields.value
  )
  
  return (
    fields.filter(field => Boolean(field))
  )
}

export function buildQualityInfoFormDataForFormView(product: Record<string, any> = {}): QualityInfoForm {
  const transformedProduct: Record<string, any> = transformProductQualityInfoTimeStampToDateString(product)
  return convertQualityInfoRemoteDataToFormQualityInfoFieldValue(transformedProduct, true)
}

export function transformProductQualityInfoTimeStampToDateString(product: Record<string, any> = {}) {
  const qualityStartTime: string = product?.qualityStartTime || ''
  const qualityEndTime: string = product?.qualityEndTime || ''
  
  const qualityInfoTime: Record<string, string> = {
    qualityStartTime: qualityStartTime ? moment(qualityStartTime).format(DateFormatEnum.YMD_UP) : '',
    qualityEndTime: qualityEndTime ? moment(qualityEndTime).format(DateFormatEnum.YMD_UP) : ''
  }
  
  return Object.assign(product, {})
}

export function isQualityStatusIn(status: string) {
  return status == QualityInfoStateValueEnum.保内 || status == QualityInfoStateEnum.In
}

export default {
  isAutoComputedQualityInfo,
  isManualAddQualityInfo,
  convertQualityInfoRemoteDataToFormQualityInfoFieldValue,
  convertFormQualityInfoFieldValueToQualityInfoRemoteData,
  buildQualityInfoForm,
  smoothQualityInfoFieldForFormView,
  smoothQualityInfoFieldForTable,
  isQualityStatusIn
}